; waplugin.nsi
;
; This script will generate an installer that installs a Winamp 2 plug-in.
;
; This installer will automatically alert the user that installation was
; successful, and ask them whether or not they would like to make the 
; plug-in the default and run Winamp.

;--------------------------------

; Uncomment the next line to enable auto Winamp download

; The name of the installer
Name "Unreal Music[umx] Plugin by andREV"
!define BUILD "20061112"
!define VERSION "0.1.2"

; The file to write
OutFile "in_unreal_winamp_plugin_${VERSION}_build${BUILD}.exe"

; The default installation directory
InstallDir $PROGRAMFILES\Winamp

; detect winamp path from uninstall string if available
InstallDirRegKey HKLM \
                 "Software\Microsoft\Windows\CurrentVersion\Uninstall\Winamp" \
                 "UninstallString"


!define MUI_WELCOMEFINISHPAGE_BITMAP "..\graph\in_unreal.bmp"
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\arrow2-install.ico"
!define MUI_UNICON  "${NSISDIR}\Contrib\Graphics\Icons\arrow2-uninstall.ico"

; The text to prompt the user to enter a directory
DirText "Please select your Winamp path below (you will be able to proceed when Winamp is detected):"
# currently doesn't work - DirShow hide

; automatically close the installer when done.
; utoCloseWindow true

; hide the "show details" box
; ShowInstDetails nevershow

;--------------------------------

!include "MUI.nsh"
  !insertmacro MUI_PAGE_WELCOME
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES
  
  
  !insertmacro MUI_LANGUAGE "Polish"
  !insertmacro MUI_LANGUAGE "English"
;Pages
;Page directory
;Page instfiles


  
;--------------------------------

; The stuff to install

Section ""
  call GetWinampInstPath
  StrCpy $1 $INSTDIR\Plugins
             
  SetOutPath $1
  
  ; File to extract
  File ..\bin\in_unreal.dll

  ; prompt user, and if they select no, go to NoWinamp
  MessageBox MB_YESNO|MB_ICONQUESTION \
             "The plug-in was installed. Would you like to run Winamp now ?" \
             IDNO NoWinamp
    Exec '"$INSTDIR\Winamp.exe"'
  NoWinamp:
  
SectionEnd

;--------------------------------

Function GetWinampInstPath

  Push $0
  Push $1
  Push $2
  ReadRegStr $0 HKLM \
     "Software\Microsoft\Windows\CurrentVersion\Uninstall\Winamp" \ 
     "UninstallString"
  StrCmp $0 "" fin

    StrCpy $1 $0 1 0 ; get firstchar
    StrCmp $1 '"' "" getparent 
      ; if first char is ", let's remove "'s first.
      StrCpy $0 $0 "" 1
      StrCpy $1 0
      rqloop:
        StrCpy $2 $0 1 $1
        StrCmp $2 '"' rqdone
        StrCmp $2 "" rqdone
        IntOp $1 $1 + 1
        Goto rqloop
      rqdone:
      StrCpy $0 $0 $1
    getparent:
    ; the uninstall string goes to an EXE, let's get the directory.
    StrCpy $1 -1
    gploop:
      StrCpy $2 $0 1 $1
      StrCmp $2 "" gpexit
      StrCmp $2 "\" gpexit
      IntOp $1 $1 - 1
      Goto gploop
    gpexit:
    StrCpy $0 $0 $1

    StrCmp $0 "" fin
    IfFileExists $0\winamp.exe fin
      StrCpy $0 ""
  fin:
  Pop $2
  Pop $1
 ; Exch $0            
               
FunctionEnd

